/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：
* CreateTime：2020-05-11 10:26:12
* Version：1.0
* Modify Recoder：
*************************************************/

using System;
using System.Collections;
using UnityEngine;

namespace TinyToolKit
{
    [Serializable]
    public class FOVKick
    {
        public Camera camera; // 可选择设置Camera，如果没有设置，则为MainCamera
        public float fovIncrease = 3f; // 进入奔跑状态时，fov的增长值
        public float timeToIncrease = 1f; // 完成fov增涨的时间
        public float timeToDecrease = 1f; // 回到fov的初始值的时间
        public AnimationCurve increaseCurve = new AnimationCurve(new Keyframe(0,0),new Keyframe(1,1)); // fov值的增涨曲线

        [HideInInspector] public float originalFov; // fov的初始角度

        public void Setup(Camera cam)
        {
            if (cam == null || increaseCurve == null) return;
            
            this.camera = cam;
            originalFov = cam.fieldOfView;
        }
        
        // 增加视野
        public IEnumerator FOVKickUp()
        {
            float t = Mathf.Abs((camera.fieldOfView - originalFov) / fovIncrease) * timeToIncrease;
            while (t < timeToIncrease)
            {
                // 根据增涨曲线和时间计算当前的fov值，直到t值大于等于完成时间，跳出循环
                camera.fieldOfView = originalFov + (increaseCurve.Evaluate(t / timeToIncrease) * fovIncrease);
                t += Time.deltaTime;

                yield return new WaitForEndOfFrame(); // 每帧执行一次，增涨一次
            }
        }

        // 减少视野
        public IEnumerator FOVKickDown()
        {
            float t = Mathf.Abs((camera.fieldOfView - originalFov) / fovIncrease) * timeToIncrease;

            while (t > 0)
            {
                camera.fieldOfView = originalFov + (increaseCurve.Evaluate(t / timeToDecrease) * fovIncrease);
                t -= Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }
            
            camera.fieldOfView = originalFov;
        }
    }
}